package com.ruoyi.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SkplQuestion;
import com.ruoyi.system.service.ISkplQuestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问题Controller
 *
 * @author ruoyi
 * @date 2022-06-15
 */
@RestController
@RequestMapping("/system/question")
public class SkplQuestionController extends BaseController
{
    @Autowired
    private ISkplQuestionService skplQuestionService;

    /**
     * 查询问题列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(SkplQuestion skplQuestion)
    {
        startPage();
        List<SkplQuestion> list = skplQuestionService.selectSkplQuestionList(skplQuestion);
        return getDataTable(list);
    }


    /**
     * 导出问题列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SkplQuestion skplQuestion)
    {
        List<SkplQuestion> list = skplQuestionService.selectSkplQuestionList(skplQuestion);
        ExcelUtil<SkplQuestion> util = new ExcelUtil<SkplQuestion>(SkplQuestion.class);
        util.exportExcel(response, list, "问题数据");
    }

    /**
     * 获取问题详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") String questionId)
    {
        return AjaxResult.success(skplQuestionService.selectSkplQuestionByQuestionId(questionId));
    }


    /**
     * 新增问题
     */
    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "问题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SkplQuestion skplQuestion)
    {
        if(skplQuestion.getQuestionUser()==null) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            skplQuestion.setQuestionUser(user.getNickName());
            java.util.Date day =new Date();
            skplQuestion.setQuestionCreatetime(day);
        }
        return toAjax(skplQuestionService.insertSkplQuestion(skplQuestion));
    }

    /**
     * 修改问题
     */
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SkplQuestion skplQuestion)
    {
        return toAjax(skplQuestionService.updateSkplQuestion(skplQuestion));
    }

    /**
     * 删除问题
     */
    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "问题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable String[] questionIds)
    {
        return toAjax(skplQuestionService.deleteSkplQuestionByQuestionIds(questionIds));
    }
}
